﻿using Sidvall.WorkItemManager.UI.Messaging;

namespace Sidvall.WorkItemManager.UI.ViewModels.WorkItems
{
    public class WorkItemContextViewModel : Sidvall.WorkItemManager.UI.ViewModels.Core.ViewModelBase
    {
        #region Public Members

        #region CanEdit

        private bool _CanEdit;
        public bool CanEdit
        {
            get
            {
                return _CanEdit;
            }
            set
            {
                if (_CanEdit == value)
                    return;
                _CanEdit = value;
                RaisePropertyChanged(nameof(CanEdit));
            }
        }

        #endregion
        #region Context

        private string _Context;
        public string Context
        {
            get
            {
                return _Context;
            }
            set
            {
                string formattedValue;
                try
                {
                    var item = Sidvall.Serialization.SerializerManager.Current.Deserialize(value);
                    if (item != null)
                        formattedValue = Sidvall.Serialization.SerializerManager.Current.SerializeObject(item, Serialization.Formatting.Indented);
                    else
                        formattedValue = value;
                }
                catch
                {
                    formattedValue = value;
                }
                if (_Context == formattedValue)
                    return;
                _Context = formattedValue;
                RaisePropertyChanged("Context");
                OnContextChanged();
            }
        }

        #endregion

        #region OnContextChanged

        protected virtual void OnContextChanged()
        {
            if (this.PublishChange)
            {
                var message = new WorkItemContextMessage(this)
                {
                    Context = this.Context,
                    CanEdit = this.CanEdit,
                };
                Publish(message);
            }
        }

        #endregion

        #region Init

        public void Init(ContextParameter item)
        {
            this.ContextParameter = item;
        }

        #endregion
        #region InitializeContext

        public virtual void InitializeContext()
        {
        }

        #endregion
        #region Start

        public override void Start()
        {
            Subscribe<WorkItemContextMessage>(OnWorkItemContextMessage);
            if (this.ContextParameter != null)
            {
                this.PublishChange = false;
                this.Context = this.ContextParameter.Context;
                this.PublishChange = true;
                InitializeContext();
                this.ContextParameter = null;
            }
        }

        #endregion

        #endregion
        #region Private Members

        private ContextParameter ContextParameter { get; set; }
        private bool PublishChange { get; set; }

        #region OnWorkItemContextMessage

        private void OnWorkItemContextMessage(WorkItemContextMessage item)
        {
            this.CanEdit = item.CanEdit;
            if (this.Context == item.Context)
                return;
            this.PublishChange = false;
            this.Context = item.Context;
            this.PublishChange = true;
            InitializeContext();
        }

        #endregion

        #endregion
    }
}
